<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/10/12
 * Time: 14:33
 */
namespace app\index\controller;
use think\Controller;
use think\Db;

class Upload extends Controller{
    public function picture(){

        //上传文件
        $file = request()->file('img');
        $folder = "adver";
        $image_path = $this->upload($file,$folder);
        dump($image_path);die;
        $this->assign('image_path',$image_path);

        if ($file){

        }
        return $this->fetch('upload');
    }
    function upload($file,$folder){
        //获取表单上传文件
        //$file = request()->file('img');
        //移动到框架应用根目录/public/uploads/目录下
        //$info = $file->move(ROOT_PATH.'public'.DS.'uploads');
        $info = $file->validate(['size'=>815678,'ext'=>'jpg,png,gif'])->move('../uploads/',$folder);
        if ($info){
            //存入相对路径/uploads/日期/文件名
            //$data = DS.'uploads'.DS.$info->getSaveName();
            $image = $info->getSaveName();
            $images = str_replace("\\","/",$image);
            $img_path = '/uploads/'.$folder.'/'.$images;
            return $img_path;
        }else{
            //上传失败获取错误信息
            echo $file->getError();
        }
    }
}